package edu.psu.collegerecommendationhelper.coreservlets;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import edu.psu.collegerecommendationhelper.database.GenericDatabase;
import edu.psu.collegerecommendationhelper.database.MySQLDatabase;
import edu.psu.collegerecommendationhelper.model.EducationInfo;
import edu.psu.collegerecommendationhelper.model.UserBean;



@ManagedBean(name="modifyEducation")
public class ModifyEducationInfoBean {
@ManagedProperty(value="#{userBean}")
   private UserBean user;

   private float entered_GPA;
   private short entered_mathSAT;
   private short entered_creadSAT;
   private short entered_writeSAT;
   private byte entered_mathACT;
   private byte entered_readACT;
   private byte entered_englishACT;
   private byte entered_scienceACT;

   private EducationInfo educationInfo;

   private String GPA_error;
   private String mathSAT_error;
   private String creadSAT_error;
   private String writeSAT_error;
   private String mathACT_error;
   private String readACT_error;
   private String englishACT_error;
   private String scienceACT_error;


   public UserBean getUser() {
      return user;
   }
   public void setUser(UserBean user) {
      this.user = user;
   }

   public float getEntered_GPA() {
      return entered_GPA;
   }
   public void setEntered_GPA(float entered_GPA) {
      this.entered_GPA = entered_GPA;
   }

   public short getEntered_mathSAT() {
      return entered_mathSAT;
   }
   public void setEntered_mathSAT(short entered_mathSAT) {
      this.entered_mathSAT = entered_mathSAT;
   }

   public short getEntered_creadSAT() {
      return entered_creadSAT;
   }
   public void setEntered_creadSAT(short entered_creadSAT) {
      this.entered_creadSAT = entered_creadSAT;
   }

   public short getEntered_writeSAT() {
      return entered_writeSAT;
   }
   public void setEntered_writeSAT(short entered_writeSAT) {
      this.entered_writeSAT = entered_writeSAT;
   }

   public byte getEntered_mathACT() {
      return entered_mathACT;
   }
   public void setEntered_mathACT(byte entered_mathACT) {
      this.entered_mathACT = entered_mathACT;
   }

   public byte getEntered_readACT() {
      return entered_readACT;
   }
   public void setEntered_readACT(byte entered_readACT) {
      this.entered_readACT = entered_readACT;
   }

   public byte getEntered_englishACT() {
      return entered_englishACT;
   }
   public void setEntered_englishACT(byte entered_englishACT) {
      this.entered_englishACT = entered_englishACT;
   }

   public byte getEntered_scienceACT() {
      return entered_scienceACT;
   }
   public void setEntered_scienceACT(byte entered_scienceACT) {
      this.entered_scienceACT = entered_scienceACT;
   }

   public EducationInfo getEducationInfo() {
      return educationInfo;
   }
   public void setEducationInfo(EducationInfo educationInfo) {
      this.educationInfo = educationInfo;
   }

   public String getGPA_error() {
      return GPA_error;
   }

   public String getMathSAT_error() {
      return mathSAT_error;
   }

   public String getCreadSAT_error() {
      return creadSAT_error;
   }

   public String getWriteSAT_error() {
      return writeSAT_error;
   }

   public String getMathACT_error() {
      return mathACT_error;
   }

   public String getReadACT_error() {
      return readACT_error;
   }

   public String getEnglishACT_error() {
      return englishACT_error;
   }

   public String getScienceACT_error() {
      return scienceACT_error;
   }

////////////////////////////////////////////////////////////////////////


public String saveEducationInfo() {
      MySQLDatabase mysqlDB = new MySQLDatabase();
      GenericDatabase genDB;
      genDB = mysqlDB;

      boolean action = false;
      educationInfo = user.getEducationInfo();
      GPA_error = "";
      mathSAT_error = "";
      creadSAT_error = "";
      writeSAT_error = "";
      mathACT_error = "";
      readACT_error = "";
      englishACT_error = "";
      scienceACT_error = "";


      if ((entered_GPA != educationInfo.getGPA()) &&
          (0 != entered_GPA)) {
          if ((0.01 > entered_GPA) || (6.0 < entered_GPA)) {
             GPA_error = "Invalid GPA Entered (Must be between 0.01 and 6.00)";
          }
          else {
             educationInfo.setGPA(entered_GPA);
             action = true;
          }
      }


      if ((entered_mathSAT != educationInfo.getMathSAT()) &&
          (0 != entered_mathSAT)) {
         if ((1 > entered_mathSAT) || (800 < entered_mathSAT)) {
            mathSAT_error = "Invalid Math SAT Entered (Must be between 1 and 800)";
         }
         else {
            educationInfo.setMathSAT(entered_mathSAT);
            action = true;
         }
      }


      if ((entered_creadSAT != educationInfo.getCreadSAT()) &&
          (0 != entered_creadSAT)) {
         if ((1 > entered_creadSAT) || (800 < entered_creadSAT)) {
            creadSAT_error = "Invalid Critical Read SAT Entered (Must be between 1 and 800)";
         }
         else {
            educationInfo.setCreadSAT(entered_creadSAT);
            action = true;
         }
      }


      if ((entered_writeSAT != educationInfo.getWriteSAT()) &&
          (0 != entered_writeSAT)) {
         if ((1 > entered_writeSAT) || (800 < entered_writeSAT)) {
            writeSAT_error = "Invalid Writing SAT Entered (Must be between 1 and 800)";
         }
         else {
            educationInfo.setWriteSAT(entered_writeSAT);
            action = true;
         }
      }


      if ((entered_mathACT != educationInfo.getMathACT()) &&
          (0 != entered_mathACT)) {
         if ((1 > entered_mathACT) || (36 < entered_mathACT)) {
            mathACT_error = "Invalid Math ACT Entered (Must be between 1 and 36)";
         }
         else {
            educationInfo.setMathACT(entered_mathACT);
            action = true;
         }
      }


      if ((entered_readACT != educationInfo.getReadACT()) &&
          (0 != entered_readACT)) {
         if ((1 > entered_readACT) || (36 < entered_readACT)) {
            readACT_error = "Invalid Reading ACT Entered (Must be between 1 and 36)";
         }
         else {
            educationInfo.setReadACT(entered_readACT);
            action = true;
         }
      }


      if ((entered_englishACT != educationInfo.getEnglishACT()) &&
          (0 != entered_englishACT)) {
         if ((1 > entered_englishACT) || (36 < entered_englishACT)) {
            englishACT_error = "Invalid English ACT Entered (Must be between 1 and 36)";
         }
         else {
            educationInfo.setEnglishACT(entered_englishACT);
            action = true;
         }
      }


      if ((entered_scienceACT != educationInfo.getScienceACT()) &&
          (0 != entered_scienceACT)) {
         if ((1 > entered_scienceACT) || (36 < entered_scienceACT)) {
            scienceACT_error = "Invalid Science ACT Entered (Must be between 1 and 36)";
         }
         else {
            educationInfo.setScienceACT(entered_scienceACT);
            action = true;
         }
      }

      
      if((true == action) &&
         GPA_error.isEmpty() && mathSAT_error.isEmpty() &&
         creadSAT_error.isEmpty() && writeSAT_error.isEmpty() &&
         mathACT_error.isEmpty() && readACT_error.isEmpty() &&
         englishACT_error.isEmpty() && scienceACT_error.isEmpty() ) {

         user.setEducationInfo(educationInfo);
         genDB.saveEducationInfo(user.getUser_id(), educationInfo);

         return ("ModifyInfoPage");
      }
      else {
         return null;
      }
   }

}
